export const getInputs = () => [[[0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]], [[4, 2, 0, 3, 2, 5]]];

/**
 * **[42. 接雨水](https://leetcode.cn/problems/trapping-rain-water/)**
 *
 * 给定 `n` 个非负整数表示每个宽度为 `1` 的柱子的高度图，计算按此排列的柱子，下雨之后能接多少雨水。
 *
 * ![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/22/rainwatertrap.png)
 *
 * @param height
 * @returns
 */
export default function (height: number[]): number {
	const length = height.length;
	const leftMax: number[] = [0],
		rightMax: number[] = [...new Array(length - 1), 0];

	{
		let max = 0;
		for (let i = 1; i < length; i++) {
			max = Math.max(height[i - 1], max);
			leftMax.push(max);
		}
	}

	{
		let max = 0;
		for (let i = length - 2; i >= 0; i--) {
			max = Math.max(height[i + 1], max);
			rightMax[i] = max;
		}
	}

	let result = 0;
	height.forEach((hgt, idx) => {
		result += Math.max(0, Math.min(leftMax[idx], rightMax[idx]) - hgt);
	});

	return result;
}
